PREFIX = $(HOME)

target = toolkit

language = C++

src_dirs = \
	src \

#release = no

#static = yes

math = yes

verbose = yes


CFLAGS = -Wall -pedantic -pedantic-errors 

CFLAGS += -O3

LDFLAGS = -lrt

CSTD = c99

CXXSTD = c++98

ifdef release
CFLAGS += -DNDEBUG
CFLAGS += -enable-libstdcxx-debug
else
CFLAGS += -ggdb -DDEBUG -fno-inline
endif

ifdef math
LDFLAGS += -lm
endif

ifdef static
LDFLAGS += -static
endif

CFLAGS += $(foreach dir, $(src_dirs), -I$(dir))

CXXFLAGS += $(CFLAGS)


ifeq ($(language), C++)
src_extension = .cpp
LD = $(CXX)
CXXFLAGS += -std=$(CXXSTD)
else
src_extension = .c
LD = $(CC)
CFLAGS += -std=$(CSTD)
endif

occ := $(CC)
ocxx := $(CXX)
orm := $(RM)
old := $(LD)
ifdef verbose
RM := $(RM) -v
else
CC =  @echo "  CC  $@"; $(occ)
CXX = @echo "  CXX $@"; $(ocxx)
RM =  @echo "  CLEAN"; $(orm)
LD =  @echo "  LD  $@"; $(old)
endif


src_files ?= \
	$(foreach dir, $(src_dirs), \
		$(shell find $(dir) -name '*$(src_extension)'))

obj_files = $(src_files:$(src_extension)=.o)

deps_files = $(obj_files:.o=.d)



.PHONY: all clean

%.d : %$(src_extension) Makefile
	@$(old) -MM -MF $@ -MP -MT $(@:.d=.o) $(CFLAGS) $<

-include $(deps_files)

all : $(target)

$(target) : $(obj_files) Makefile
	$(LD) $(LDFLAGS) $(obj_files) $(LOADLIBS) $(LDLIBS) -o $(target)

install:
	mkdir -p $(PREFIX)
	mv $(target) $(PREFIX)
	mv .fnb.conf $(PREFIX)
	mv .fnb.resume $(PREFIX)

clean:
	$(RM) $(obj_files) $(deps_files) $(target)
